package com.example.secondapplication;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class Register extends AppCompatActivity {

    private String registerUrl = "https://www.wanandroid.com/user/register";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
    }

    public void click1(View view) {
        EditText e_username = findViewById(R.id.suser);
        EditText e_pwd = findViewById(R.id.spassword);
        EditText e_repwd = findViewById(R.id.srepassword);
        String username = e_username.getText().toString();
        String pwd = e_pwd.getText().toString();
        String repwd = e_repwd.getText().toString();
        new Thread(){
            @Override
            public void run() {
                try {
                    String register = register(username,pwd,repwd);
                    JSONObject object = new JSONObject(register);
                    int errorCode = object.optInt("errorCode");
                    String errorMsg = object.optString("errorMsg");
                    if (errorCode==0){
                        System.out.println("注册成功");
                    }else {
                        System.out.println("注册失败");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void click2(View view) {
        Intent intent=new Intent();
        intent.setClass(this, MainActivity.class);
        startActivity(intent);
    }

    private String register(String username,String pwd,String repwd){
        try {
            URL url = new URL(registerUrl);
            HttpURLConnection cn = (HttpURLConnection) url.openConnection();
            cn.setRequestMethod("POST");
            cn.setDoOutput(true);
            cn.setUseCaches(false);
            String str = "username=" + URLEncoder.encode(username, "utf-8")
                    + "&password=" + URLEncoder.encode(pwd, "utf-8")
                    + "&repassword=" + URLEncoder.encode(repwd, "utf-8");
            OutputStream outputStream = cn.getOutputStream();
            outputStream.write(str.getBytes());
            outputStream.flush();
            if (cn.getResponseCode()==200){
                InputStream inputStream = cn.getInputStream();
                String s = stream2String(inputStream);
                return s;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private String stream2String(InputStream is){
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuffer sb = new StringBuffer();
        String line;
        try {
            while ((line=br.readLine()) != null) {
                sb.append(line);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return sb.toString();
    }

}